package com.sinocare.project.store.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.project.common.domain.SccDrugstore;
import com.sinocare.project.common.domain.Store;
import com.sinocare.project.common.domain.vo.StoreVo;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.store.service.StoreService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/index")
public class StoreController {
    @Autowired
    private StoreService storeService;

//    @Resource
//    private StoreMapper storeMapper;

   @ApiOperation(value = "查询所有总药店",notes = "查询所有总药店的信息",httpMethod = "GET")
   @RequestMapping(value = "all",method = RequestMethod.GET)
    public Result pageStores(Integer page,Integer limit,String searchParams)throws Exception{
       ObjectMapper objectMapper = new ObjectMapper();
       StoreVo storeVo=null;
       if (searchParams!=null){
           storeVo=objectMapper.readValue(searchParams,StoreVo.class);
       }
       Page<Object> page1 = PageHelper.startPage(page,limit);
       System.out.println(page1);
       List<Store> stores = storeService.queryStore(storeVo);
       return Result.ok(ResultCode.SUCCESS,page1.getTotal(),stores);
   }

    @ApiOperation(value = "查询所有分药店",notes = "查询所有分药店的信息",httpMethod = "GET")
    @RequestMapping(value = "find",method = RequestMethod.GET)
    public Result findStores(Integer page,Integer limit,String searchParams)throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        StoreVo storeVo=null;
        if (searchParams!=null){
            storeVo=objectMapper.readValue(searchParams,StoreVo.class);
        }
        Page<Object> page1 = PageHelper.startPage(page,limit);
        System.out.println(page1);
        List<Store> stores = storeService.selectStores(storeVo);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),stores);
    }



    @ApiOperation(value = "查询所有总店名",notes = "查询出所有总店名",httpMethod = "GET")
    @RequestMapping(value = "stname",method = RequestMethod.GET)
    public Result selectStoreNames()throws Exception{
        List<Store> s = storeService.selectStoreName();
        return Result.ok(ResultCode.SUCCESS,s);
    }


    @ApiOperation(value = "查看某个总店下的所有分店",notes = "查询出某个总店下的所有分店信息",httpMethod = "GET")
    @RequestMapping(value = "substore/{storeCode}",method = RequestMethod.GET)
    public Result subStore(@PathVariable("storeCode") String storeCode,Integer page, Integer limit,String searchParams)throws Exception{

        ObjectMapper objectMapper = new ObjectMapper();
        StoreVo storeVo=null;
        if (searchParams!=null){
            storeVo=objectMapper.readValue(searchParams,StoreVo.class);
        }
        Page<Object> page1 = PageHelper.startPage(page,limit);
        System.out.println(page1);
        List<Store> sccDrugstores = storeService.subStoreList(storeCode, storeVo);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),sccDrugstores);
    }

}
